package com.xiaoyg.send;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.UUID;

/**
 * @author xiaoyg
 * @Title: SendLogTopic
 * @ProjectName rabbitmq-topic
 * @Description: TODO
 * @date 2018/12/418:56
 */
public class SendLogTopic {

    private static final String EXCHANGE_NAME = "ex_topic_logs";

    public static void main(String[] args) throws IOException {

        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setUsername("admin");
        connectionFactory.setPassword("123456");
        connectionFactory.setHost("192.168.8.129");
        connectionFactory.setPort(AMQP.PROTOCOL.PORT);

        Connection connection = connectionFactory.newConnection();

        Channel channel = connection.createChannel();

        channel.exchangeDeclare(EXCHANGE_NAME,"topic");
        String [] routing_keys = new String[]{"xiaoyg.info","xuyj.warning","auth.info","xiaoyg.critical"};

        for (String routing_key:routing_keys) {
            String msg = UUID.randomUUID().toString();
            channel.basicPublish(EXCHANGE_NAME,routing_key,null,msg.getBytes());

            System.out.println(" [x] send routingKey ="+routing_key+",msg = "+msg.getBytes());
        }

        channel.close();
        connection.close();
    }

}
